﻿using System;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using Systemathics.FrameWork;

namespace Systemathics.UserControls.OrderBox
{
    [ToolboxBitmap(typeof(OrderBoxControl), "UserControlsIcon.bmp")]
    public sealed partial class OrderBoxControl : UserControl
    {
        private OrderSide Side;
        private OrderStyle Style;
        private Instrument instr;
        public Instrument Instrument
        {
            set
            {
                instr = value;
                Instrumentlabel.Text = instr.StaticData.Name;
                Exchangelabel.Text = instr.StaticData.Exchange.Name;

                if (SideLabel.Text == "BUY")
                {
                    Side = OrderSide.Buy;
                    NewPriceTextBox.Text = instr.Blotter[QuoteLevel.One].Ask.ToString();
                    NewQtyTextBox.Text = instr.Blotter[QuoteLevel.One].AskSize.ToString();
                }
                else if (SideLabel.Text == "SELL")
                {
                    Side = OrderSide.Sell;
                    NewPriceTextBox.Text = instr.Blotter[QuoteLevel.One].Bid.ToString();
                    NewQtyTextBox.Text = instr.Blotter[QuoteLevel.One].BidSize.ToString();
                }
            }
        }
        public Order Order { get; private set; }
        private readonly OrderExtension Ext;


        public OrderBoxControl()
        {
            InitializeComponent();
            DoubleBuffered = true;
            Dock = DockStyle.Fill;
            Thread.CurrentThread.CurrentCulture = GlobalVariables.SystemathicsCulture();
            OrderStyleComboBox.DataSource = Enum.GetValues(typeof(OrderStyle));
            TIFComboBox.DataSource = Enum.GetValues(typeof(TimeInForce));

            OrderStyleComboBox.SelectedItem = OrderStyle.Limit;
            TIFComboBox.SelectedItem = TimeInForce.Day;
            Ext = new OrderExtension();
        }


        private void Executebutton_Click(object sender, EventArgs e)
        {
            Decimal price;
            int qty;
            if (Decimal.TryParse(NewPriceTextBox.Text, NumberStyles.Float, GlobalVariables.SystemathicsCulture(), out price)
                && int.TryParse(NewQtyTextBox.Text, NumberStyles.Integer, GlobalVariables.SystemathicsCulture(), out qty))
            {
                Decimal stopprice = 0;
                if (Style == OrderStyle.StopLimit  || Style == OrderStyle.TrailingStopLimit )
                    if (Decimal.TryParse(StopPriceTextBox.Text, NumberStyles.Float, GlobalVariables.SystemathicsCulture(), out stopprice))
                        Order.TargetPrice = stopprice;

                Order = new Order(instr.Id, instr.StaticData, Style, Side,price,qty, Guid.Empty) {Ext = Ext};
                if (ParentForm != null)
                    ParentForm.DialogResult = DialogResult.OK;
            }
            else
                new RemotingLogClient().Trace(new FireMessageEventArgs("Order Qty Or Price invalid in OrderBox Control",DateTime.Now.TimeOfDay));
        }

        private void TIFComboBox_DropDownClosed(object sender, EventArgs e)
        {
            Ext.Tif = (TimeInForce)Enum.Parse(typeof(TimeInForce), TIFComboBox.SelectedItem.ToString());
            if(Ext.Tif == TimeInForce.GoodTillDate)
                TifDateTimePicker.Enabled = true;
        }

        private void OrderStyleComboBox_DropDownClosed(object sender, EventArgs e)
        {
            Style = (OrderStyle)Enum.Parse(typeof(OrderStyle), OrderStyleComboBox.SelectedItem.ToString());
        }

        private void OutSideHoursCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Ext.OutsideRth = (OutSideHoursCheckBox.Checked) ? true : false;
        }

        private void Executebutton_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                Executebutton_Click(this, new EventArgs());
        }

        private void TifDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            Ext.GoodTillDate = TifDateTimePicker.Value;
        }

        private void SweepToFillcheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Ext.SweepToFill = (OutSideHoursCheckBox.Checked) ? true : false;
            if(Ext.SweepToFill)
            {
                TifDateTimePicker.Enabled = false;
                OrderStyleComboBox.Enabled = false;
                TIFComboBox.Enabled = false;
                NewPriceTextBox.Enabled = false;
                NewQtyTextBox.Enabled = false;
                StopPriceTextBox.Enabled = false;
            }
            else
            {
                TifDateTimePicker.Enabled = true;
                OrderStyleComboBox.Enabled = true;
                TIFComboBox.Enabled = true;
                NewPriceTextBox.Enabled = true;
                NewQtyTextBox.Enabled = true;
                StopPriceTextBox.Enabled = true;
            }
        }
    }
}
